<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning" @click="$router.push('/import')">导入excel</el-button>
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <!--
            el-table 绑定数据 :data="list"
            el-table-column 绑定数据对应的属性值  prop="属性值"-->
        <!-- v-loading 是element ui提供的 -->
        <el-table
          v-loading="loading"
          border
          :data="list"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
        >
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="头像" align="center" width="120px">
            <template slot-scope="{row}">
              <!-- :src="row.staffPhoto " -->
              <!-- row.staffPhoto || '' 返回null时候 src直接没有了 -->
              <img
                slot="reference"
                v-imgerror="require('@/assets/common/bigUserHeader.png')"
                :src="row.staffPhoto || ''"
                style="border-radius: 50%; width: 100px; height: 100px; padding: 10px"
                alt=""
                @click="showQrCode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" :formatter="formatterFormOfEmployment" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="">
            <template v-slot="{row}">
              <span>{{ row.timeOfEntry | formatDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState">
            <!-- :active-value 打开
                :inactive-value 关闭 -->
            <template v-slot="obj">
              <el-switch
                :value="(obj.row.enableState ===1)"
                active-color="blue"
                inactive-color="pink"
              />
            </template>
          </el-table-column>

          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="editRole(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="deleteEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="prev, pager, next"
            :total="page.total"
            :page-size="page.size"
            :current-page.sync="page.page"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <!-- props props名称 + emit update:props名称 -->
    <AddEmployee :show-dialog.sync="showDialog" />
    <el-dialog title="二维码" :visible.sync="showCodeDialog">
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>
    <assign-role ref="assignRole" :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
  </div>
</template>

<script>

// 枚举值：0 -> 否/成功 1 -> 是/失败
// 状态：active -> 启用 forbidden -> 禁用
// 给后端传英文或者数字，枚举值一般都是后端定义，前后端联调枚举值怎么定义的
// 枚举值 一般会提取到一个文件中去写而不是直接在页面或者组件去写
// 枚举值复用性 -> 提取到一个文件中
import { getEmployeeList, deleteEmployee } from '@/api/employees'
import EmployeesConstant from '@/api/constant/employees'
import AddEmployee from './components/add-employee.vue'
import QrCode from 'qrcode'
import assignRole from './components/assign-role.vue'
export default {
  components: { AddEmployee, assignRole },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      loading: false,
      showDialog: false,
      showCodeDialog: false,
      showRoleDialog: false,
      userId: null
    }
  },
  created() {
    this.getEmployeeList()
    // console.log('created 父亲')
  },
  mounted() {
    // console.log(this.$children) // 获取的是组件，不是子元素 应该写在mounted里面，created子组件还未挂载
  },
  methods: {
    async getEmployeeList() {
      this.loading = true // 请求前设置成true
      const { total, rows } = await getEmployeeList(this.page)
      this.list = rows // 表格数据
      this.page.total = total
      this.loading = false
    },
    changePage() {
      this.getEmployeeList()
    },
    formatterFormOfEmployment(row, column, cellValue, index) {
      //   cellValue // 表格行的值
      const obj = EmployeesConstant.hireType.find(item => item.id === cellValue) // 未找到会返回undefined，找到了返回当前item
      return obj ? obj.value : '未知'
    },
    // 1. 提示信息
    // 2. 调删除接口
    // 3. 重新拉取数据
    async  deleteEmployee(id) {
      try {
        await this.$confirm('您确定删除这个员工吗？')
        await deleteEmployee(id)
        await this.getEmployeeList()
        // Vue.prototype
        this.$message.success('删除成功啦！')
      } catch (error) {
        console.log(error)
      }
    },
    addEmployee() {
      this.showDialog = true
    },
    showQrCode(url) {
      if (url) {
        this.showCodeDialog = true // 数据更新是异步
        // 第一个参数为dom元素，第二个为二维码信息
        // nextTick 获取数据更新后的值
        this.$nextTick(() => {
          QrCode.toCanvas(this.$refs.myCanvas, url)
        })
      } else {
        this.$message.warning('图片没有地址信息！')
      }
    },
    editRole(id) {
      this.userId = id
      this.showRoleDialog = true
      this.$refs.assignRole.getUserDetailById(id)
    }
  }
}
</script>

<style>

</style>
